﻿using PPSMS_Model;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace PPSMS_DAL;

public class InverterStatus_DAL : ICRUD_DAL<InverterStatus, long>
{
    /// <summary>
    /// 删除指定ID的记录
    /// </summary>
    /// <param name="ids">逆变器状态表id数组</param>
    public void DeleteByIds(params long[] ids)
    {
        string sql = $"delete from INVERTER_STATUS where ID in ({string.Join(",", ids.Select(x => $"'{x}'"))})";
        DBHelper.ExecuteNonQuery(sql);
    }

    /// <summary>
    /// 获取当前最大ID值
    /// </summary>
    /// <returns>ID</returns>
    public long GetCurrentValue()
    {
        string sql = "select rowid from INVERTER_STATISTICS";
        return DBHelper.ExecuteQuery(sql, DBHelper.ExecuteScalarToInt, new object[0]);
    }

    /// <summary>
    /// 新增记录
    /// </summary>
    /// <param name="t">逆变器状态表对象</param>
    public void Insert(InverterStatus t)
    {
        string sql = "insert into INVERTER_STATUS (INVERTERID,STATUS,TIMESTAMP,ARRAY_VOLTAGE,ARRAY_CURRENT,GRID_VOLTAGE,CURRENT_A,CURRENT_B,CURRENT_C,FREQUENCY,INTERNAL_TEMPERATURE,CURRENT_POWER,AVERAGE_POWER) values(?,?,?,?,?,?,?,?,?,?,?,?,?)";
        DBHelper.ExecuteNonQuery(sql, t.InverterID, t.Status, t.Timestamp, t.ArrayVoltage, t.ArrayCurrent, t.GridVoltage, t.CurrentA, t.CurrentB, t.CurrentC, t.Frequency, t.InternalTemperature, t.CurrentPower, t.AveragePower);

    }

    /// <summary>
    /// 将一个 DataRow 对象转换为对应的 InverterStatus 对象
    /// </summary>
    /// <param name="dr"></param>
    /// <returns>逆变器状态表对象</returns>
    public InverterStatus ResultHandler(DataRow dr)
    {
        return new InverterStatus
        {
            InverterID = dr.Field<long>("INVERTERID"),
            Status = dr.Field<string>("STATUS"),
            Timestamp = dr.Field<string>("TIMESTAMP"),
            ArrayVoltage = dr.Field<double>("ARRAY_VOLTAGE"),
            ArrayCurrent = dr.Field<double>("ARRAY_CURRENT"),
            GridVoltage = dr.Field<double>("GRID_VOLTAGE"),
            CurrentA = dr.Field<double>("CURRENT_A"),
            CurrentB = dr.Field<double>("CURRENT_B"),
            CurrentC = dr.Field<double>("CURRENT_C"),
            Frequency = dr.Field<double>("FREQUENCY"),
            InternalTemperature = dr.Field<double>("INTERNAL_TEMPERATURE"),
            CurrentPower = dr.Field<double>("CURRENT_POWER"),
            AveragePower = dr.Field<double>("AVERAGE_POWER")
        };
    }

    /// <summary>
    /// 将一个 DataTable 对象转换为对应的 List<InverterStatus> 对象
    /// </summary>
    /// <param name="dr"></param>
    /// <returns>一个包含所有逆变器状态表信息的List</returns>
    public List<InverterStatus> ResultHandlerList(DataTable dr)
    {
        List<InverterStatus> list = new List<InverterStatus>();
        foreach (DataRow item in dr.Rows)
        {
            list.Add(ResultHandler(item));
        }
        return list;
    }

    /// <summary>
    /// 获取所有逆变器状态表的记录
    /// </summary>
    /// <returns>一个包含所有逆变器状态表信息的List</returns>
    public List<InverterStatus> SelectAll()
    {
        string sql = "select * from INVERTER_STATUS";
        return DBHelper.ExecuteQuery(sql, ResultHandler, new object[0]);
    }

    /// <summary>
    /// 根据ID获取逆变器状态表的记录
    /// </summary>
    /// <param name="id"></param>
    /// <returns>逆变器状态表对象</returns>
    public InverterStatus? SelectById(long id)
    {
        string sql = "select * from INVERTER_STATUS";
        return SelectAll().FirstOrDefault(x => x.ID == id);
    }

    /// <summary>
    ///  获取指定日期范围内的逆变器状态表的记录
    /// </summary>
    /// <param name="date"></param>
    /// <returns></returns>
    public List<InverterStatus> SelectAllByDate(string Timestamp)
    {
        string sql = $"select * from INVERTER_STATUS";
        sql += $" where Timestamp like '{Timestamp}%' ORDER BY Timestamp ASC";
        return DBHelper.ExecuteQuery(sql, ResultHandler, new object[0]);
    }

    public List<InverterStatus> SelectAllByDate(string Timestamp, int Id)
    {
        string sql = $"select * from INVERTER_STATUS";
        sql += $" where  INVERTERID = '{Id}' and Timestamp like '{Timestamp}%' ORDER BY Timestamp ASC";
        return DBHelper.ExecuteQuery(sql, ResultHandler, new object[0]);
    }

    /// <summary>
    /// 根据条件修改逆变器状态表的记录
    /// </summary>
    /// <param name="t">新的逆变器状态表对象</param>
    /// <param name="id">旧的ID</param>
    /// <exception cref="ArgumentException"></exception>
    public void UpdateById(InverterStatus t, long id)
    {
        string sql = "updata INVERTER_STATISTICS set ";
        var list = new List<object>();
        if (t.InverterID != null)
        {
            sql += "INVERTERID = ? ,";
            list.Add(t.InverterID);
        }
        if (t.Status != null)
        {
            sql += "STATUS = ? ,";
            list.Add(t.Status);
        }
        if (t.Timestamp != null)
        {
            sql += "TIMESTAMP = ? ,";
            list.Add(t.Timestamp);
        }
        if (t.ArrayVoltage != null)
        {
            sql += "ARRAY_VOLTAGE = ? ,";
            list.Add(t.ArrayVoltage);
        }
        if (t.ArrayCurrent != null)
        {
            sql += "ARRAY_CURRENT = ? ,";
            list.Add(t.ArrayCurrent);
        }
        if (t.GridVoltage != null)
        {
            sql += "GRID_VOLTAGE = ? ,";
        }
        if (t.CurrentA != null)
        {
            sql += "CURRENT_A = ? ,";
            list.Add(t.CurrentA);
        }
        if (t.CurrentB != null)
        {
            sql += "CURRENT_B = ? ,";
            list.Add(t.CurrentB);
        }
        if (t.CurrentC != null)
        {
            sql += "CURRENT_C = ? ,";
            list.Add(t.CurrentC);
        }
        if (t.Frequency != null)
        {
            sql += "FREQUENCY = ? ,";
            list.Add(t.Frequency);
        }
        if (t.CurrentPower != null)
        {
            sql += "CURRENT_POWER = ? ,";
            list.Add(t.CurrentPower);
        }
        if (t.AveragePower != null)
        {
            sql += "AVERAGE_POWER = ? ,";
        }
        sql = sql.TrimEnd(',');
        if (list.Count == 0) throw new ArgumentException("没有任何属性需要更新");
        sql += $" where ID = ?";
        list.Add(id);
        DBHelper.ExecuteNonQuery(sql, list.ToArray());
    }
    public InverterStatus? SelectById(long id, string Timestamp)
    {
        string sql = $"SELECT *,MAX(CURRENT_POWER) FROM INVERTER_STATUS WHERE INVERTERID={id}";
        if (!string.IsNullOrEmpty(Timestamp))
        {
            sql += $" AND TIMESTAMP LIKE '{Timestamp}%' GROUP BY TIMESTAMP";
        }
        return DBHelper.ExecuteQuery(sql, ResultHandler, new object[0]).FirstOrDefault();
    }
}
